/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.isel.beans;

import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author c0rp
 */
@ManagedBean(name = "user")
@SessionScoped
public class UserController implements Serializable  {
    //tomcat roles

    boolean schedule = false;
    boolean appointment = false;
    boolean userAdd = false;
    boolean patientManagment = false;
    boolean doctorManagment = false;
    boolean other = false;
    boolean isManager_script = false;
    boolean isAdmin = false;
    boolean isManager_gui = false;
    boolean isAdmin_gui = false;
    boolean isTomcat = false;
    //application roles
    boolean isManager = false;
    boolean isDoctor = false;
    boolean isReceptionist = false;
    Boolean loggedIn = false;
    Boolean fail = false;
    String username;

    public boolean isSchedule() {
        return schedule;
    }

    public void setSchedule(boolean schedule) {
        this.schedule = schedule;
    }

    public boolean isUserAdd() {
        return userAdd;
    }

    public void setUserAdd(boolean userAdd) {
        this.userAdd = userAdd;
    }

    public boolean isAppointment() {
        return appointment;
    }

    public void setAppointment(boolean appointment) {
        this.appointment = appointment;
    }

    public boolean isDoctorManagment() {
        return doctorManagment;
    }

    public void setDoctorManagment(boolean doctorManagment) {
        this.doctorManagment = doctorManagment;
    }

    public boolean isOther() {
        return other;
    }

    public void setOther(boolean other) {
        this.other = other;
    }

    public boolean isPatientManagment() {
        return patientManagment;
    }

    public void setPatientManagment(boolean patientManagment) {
        this.patientManagment = patientManagment;
    }

    public Boolean getFail() {
        return fail;
    }

    public boolean isIsAdmin() {
        return isAdmin;
    }

    public boolean isIsAdmin_gui() {
        return isAdmin_gui;
    }

    public boolean isIsDoctor() {
        return isDoctor;
    }

    public boolean isIsManager() {
        return isManager;
    }

    public boolean isIsManager_gui() {
        return isManager_gui;
    }

    public boolean isIsManager_script() {
        return isManager_script;
    }

    public boolean isIsReceptionist() {
        return isReceptionist;
    }

    public boolean isIsTomcat() {
        return isTomcat;
    }

    public Boolean getLoggedIn() {
        return loggedIn;
    }

    public UserController() {
    }

    public String getUsername() {
        return username;
    }

    public void loggedIn() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        username = etx.getUserPrincipal().getName();
        System.out.println("etx = " + etx.getApplicationMap());
        isAdmin = etx.isUserInRole("admin");
        isTomcat = etx.isUserInRole("tomcat");
        isManager_gui = etx.isUserInRole("manager-gui");
        isManager_script = etx.isUserInRole("manager-script");
        isAdmin_gui = etx.isUserInRole("admin-gui");
        isManager = etx.isUserInRole("manager");
        isDoctor = etx.isUserInRole("doctor");
        isReceptionist = etx.isUserInRole("receptionist");
        this.loggedIn = true;
    }

    public String logout() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        HttpSession session = (HttpSession) etx.getSession(false);
        session.invalidate();
        loggedIn = false;
        return "logout";
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public boolean isNotLoggenIn() {
        return !loggedIn;
    }

    public void showAppointment() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        etx.redirect("welcome.xhtml");
        appointment = true;
        patientManagment = false;
        doctorManagment = false;
        other = false;
        userAdd = false;
        schedule = false;
    }

    public boolean showAppointmentMenu() {
        if (isReceptionist || isAdmin || isManager || isDoctor) {
            return true;
        }
        return false;
    }

    public boolean showPatientManagmentMenu() {
        if (isReceptionist || isAdmin || isManager) {
            return true;
        }
        return false;
    }

    public void showPatientManagment() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        etx.redirect("welcome.xhtml");
        appointment = false;
        patientManagment = true;
        doctorManagment = false;
        other = false;
        userAdd = false;
        schedule = false;
    }

    public boolean showDoctorManagmentMenu() {
        if (isReceptionist || isAdmin || isManager) {
            return true;
        }
        return false;

    }

    public void showDoctorManagment() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        etx.redirect("welcome.xhtml");
        appointment = false;
        patientManagment = false;
        doctorManagment = true;
        other = false;
        userAdd = false;
        schedule = false;

    }

    public boolean showUserAddManagmentMenu() {
        if (isAdmin || isManager) {
            return true;
        }
        return false;

    }

    public void showUserAddManagment() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        etx.redirect("welcome.xhtml");
        appointment = false;
        patientManagment = false;
        doctorManagment = false;
        other = false;
        userAdd = true;
        schedule = false;

    }

    public boolean showScheduleMenu() {
        if (isAdmin || isManager || isDoctor || isReceptionist) {
            return true;
        }
        return false;

    }

    public void showSchedule() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext etx = ctx.getExternalContext();
        etx.redirect("welcome.xhtml");
        appointment = false;
        patientManagment = false;
        doctorManagment = false;
        other = false;
        userAdd = false;
        schedule = true;

    }

    public void showOther() {
    }
}
